package com.htzz.collection.collection;

import com.htzz.collection.config.SystemConfig;
import com.htzz.collection.domain.CollectionDataDto;
import com.htzz.collection.domain.CollectionStateDataDto;
import com.htzz.collection.domain.ResultVO;
import com.htzz.collection.service.CollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "数据采集接口")
@RestController
@Slf4j
@RequestMapping("/collection/")
public class DataCollectionController {

    @Resource
    private CollectionService collectionService;

    @ApiOperation("数据信息采集")
    @PostMapping("data_acquire")
    public ResultVO collectionData(@Validated @RequestBody CollectionDataDto collectionDataDto){
        log.info("==========data_acquire accept data start========");
        Map<String,Object> rmap = new HashMap<>();
        try {
            collectionService.dataAcquire(collectionDataDto);
            log.info("==========data_acquire accept data end========");
            return ResultVO.ok();
        } catch (Exception e) {
            log.error(" **** data_acquire has exception **** ",e);
            return ResultVO.error(e.getMessage());
        }
    }

    @ApiOperation("状态信息采集")
    @PostMapping("device_state_acquire")
    public ResultVO collectionStateData(@Validated @RequestBody CollectionStateDataDto collectionStateDataDto){
        log.info("==========device_state_acquire accept data start========");
        Map<String,Object> rmap = new HashMap<>();
        try {
            collectionService.dataStateAcquire(collectionStateDataDto);
            log.info("==========device_state_acquire accept data end========");
            return ResultVO.ok();
        } catch (Exception e) {
            log.error(" **** device_state_acquire has exception **** ",e);
            return ResultVO.error(e.getMessage());
        }
    }


    @Value("${mqtt.broker.serverUri}")
    private String MqttUrl;
    @Value("${mqtt.topic}")
    protected String topic;
    @Value("${mqtt.qos}")
    protected int qos;
    @Value("${mqtt.use}")
    protected Boolean useMqtt;

    @ApiOperation("mqtt进行数据采集的说明")
    @GetMapping("mqtt_acquire")
    public ResultVO MqttData(){
        log.info("==========mqtt_acquire========");
        Map<String,Object> rmap = new HashMap<>();
        rmap.put("useMqtt","当前mqtt是否启用："+useMqtt);
        rmap.put("description","当前可接受"+MqttUrl+"服务器上的，topic:"+topic+",qos:"+qos+"的数据采集消息");
        rmap.put("example","消息模板：{\"devId\":\"sn19990111\",\"t\": 1832893283813,\"data\":{\"speed\": \"3232\",\"cc\":\"ddd\"}}");

        log.info("==========mqtt_acquire end========");
        return ResultVO.ok(rmap);
    }
}
